<?php

namespace App\Http\Controllers;

use App\GoodDefaultClass;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\GoodStore;
use App\GoodStoreMember;
use Validator;
use App\Http\Requests;

class GoodStoreController extends Controller
{
    //
    public function create (Request $request) {
        $validator = Validator::make($request->all(),  [
            'name' => 'required|max:255',
            'type' => 'required|integer',
            'address' => 'required',
            'telephone' => 'required|max:255',
            'license' => 'required',
            'userId' => 'required|max:255',
            'sessionId' => 'required|max:255'
        ]);
        if ($validator->fails()) {
            $this->validatorError($validator);
            return ;
        }
        if (!$this->sessionValidate($request->sessionId)) {
            return ;
        }

        $member = DB::table('good_store_members')
            ->where('memberId', $request->userId)
            ->first();
        if ($member != null) {
            echo json_encode(['status' => 403, 'errmsg' => 'you has already created a store', 'data' => []]);
            return ;
        }

        // create a new store
        $store = new GoodStore();
        $store->ownerId = $request->userId;
        $store->name = $request->name;
        $store->type = $request->type;
        $store->address = $request->address;
        $store->telephone = $request->telephone;
        $store->phone = $request->phone;
        $store->introduction = $request->introduction;
        $store->license = $this->addPicture($request->license);
        if ($request->picture) 
            $store->picture = $this->addPicture($request->picture);
        
        $store->save();

        // let the user be a manager.
        $member = new GoodStoreMember();
        $member->userId = $request->userId;
        $member->name = "manager";
        $member->memberId = $request->userId;
        $member->storeId = $store->id;
        $member->identity = 0;
        $member->save();
        
        // create a default class
        $class = new GoodDefaultClass();
        $class->storeId = $store->id;
        $class->name = '默认分类';
        $class->save();

        echo json_encode(['status' => 200, 'errmsg' => '', 'data' => []]);
    }

    public function find(Request $request) {
        $validator = Validator::make($request->all(), array(
            'userId' => 'required|max:255',
            'sessionId' => 'required|max:255'
        ));
        if ($validator->fails()) {
            $this->validatorError($validator);
            return ;
        }
        if (!$this->sessionValidate($request->sessionId))
            return ;

        $member = DB::table('good_store_members')
            ->where('memberId', $request->userId)
            ->first();
        if ($member == null) {
            echo json_encode(['status' => 403, 'errmsg' => 'store not found', 'data' => []]);
            return ;
        }
        $store = GoodStore::find($member->storeId);
        echo json_encode(['status' => 200, 'errmsg' => '', 'data' => $store]);
    }
}
